<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use App\Service\Code\Normal;
use Hyperf\DbConnection\Db;

class ShopTopics extends BasePssModel
{
    public $table = 'shop_topics';
    /**
     * 专题馆
     * @param $param
     */
    public function getAllShowTopicsList()
    {
        $getHomeShowConfigData = ShopHomeShow::instance()->getHomeShowConfigDataByShowType([ShopHomeShow::TOPIC]);
        if( empty($getHomeShowConfigData) ) {
            return Normal::returnCode(Normal::FAIL,"获取基础配置为空，请先进行基础配置");
        }
        
        $current = current($getHomeShowConfigData);
        $topics = @json_decode($current['value'],true);
        if( empty($topics) ) {
            return Normal::returnCode(Normal::FAIL,"专题馆设置数据解析异常");
        }
        
        $useAbleToplist = $this->whereIn('id',$topics)
        ->where('topic_status',1)
        ->where('is_use',1)
        ->select([Db::raw("CONCAT('',id) as topic_id"),'topic_template_number'])
        ->orderBy("sort","desc")
        ->orderBy("cd","desc")
        ->get()
        ->toArray();
        if( empty($useAbleToplist) ) {
            return Normal::returnCode(Normal::FAIL,"未获取到可用的专题馆配置");
        }
        
        $topicIds       = array_column($useAbleToplist,'topic_id');
        $topConfiglist  = ShopTopicsConfiginfo::instance()->getTopicListInfoByTopicIds($topicIds);
        if( empty($topConfiglist) ) {
            return Normal::returnCode(Normal::FAIL,"未获取到可用的专题馆数据异常");
        }
        $topConfiglistMap = [];
        foreach ($topConfiglist as $item ) {
            $topConfiglistMap[$item['topic_id']][] = $item;
            unset($item);
        }
        
        $template_number = current($useAbleToplist)['topic_template_number'];
        foreach ($useAbleToplist as & $item ) {
            $topic_id = $item['topic_id'];
            if( !isset($topConfiglistMap[$topic_id]) ) {
                unset($item);
                continue;
            }
            $item['dataList'] = $topConfiglistMap[$topic_id];
        }
        
        //模板ID
        $template_id = current($useAbleToplist)['topic_template_number'];
        return Normal::returnCode(Normal::SUC,"获取数据成功",[
            'template_id'   => $template_id,
            'topicList'     => $useAbleToplist,
            'sort'          => $current['sort']
        ]);
    }
    
    /**
     * 获取banner轮播清单
     * @return mixed
     */
    public function getBannerModuleList()
    {
        $list = ShopTopicsConfiginfo::instance()->getTopicListInfoByTopicIds([0]);
        return Normal::returnCode(Normal::SUC,"获取数据成功",[
            'bannerList' => $list
        ]);
    }
}
